/*
 * @Author: wuqingchun
 * @Date: 2024-07-12 14:09:45
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-07-12 14:22:43
 */

#include <iostream>
#include <pthread.h>

class CDestructFunction
{
public:
    CDestructFunction()
    {
        std::cout << "Call construct function" << std::endl;
    }

    ~CDestructFunction()
    {
        std::cout << "Call destruct function" << std::endl;
    }

    void ThrowException()
    {
        throw "Custom exception";
    }
};

class CAppLock
{
public:
    CAppLock(pthread_mutex_t& refMtx):
        m_refMtx(refMtx)
    {
        std::cout << "CAppLock()" << std::endl;
        pthread_mutex_lock(&m_refMtx);
    }

    ~CAppLock()
    {
        pthread_mutex_unlock(&m_refMtx);
        std::cout << "~CAppLock()" << std::endl;
    }

private:
    pthread_mutex_t& m_refMtx;
};

int main(int argc, char**argv)
{
    std::cout << "Process enter" << std::endl;

    pthread_mutex_t stMtx;
    pthread_mutex_init(&stMtx, nullptr);

    CAppLock objLock(stMtx);
    
    CDestructFunction objApp;

    objApp.ThrowException();

    std::cout << "Process exit" << std::endl;

    return 0;
}